
const app = getApp()

const request = (url, options) => {
   return new Promise((resolve, reject) => {
      wx.showLoading({
        title: "请求中...",
      })
      var host = 'https://api.meetcoaching.cn/v1/'
       wx.request({
           url: host+`${url}`,
           method: options.method,
           data: options.method === 'GET' ? options.data : JSON.stringify(options.data),
           header: {
               'Content-Type': 'application/json',
               'Accept':'application/json',
              //  'Host':'<calculated when request is sent>'
           },
           success(request) {
                wx.hideLoading()
               if (request.data.code == 200) {
                   resolve(request.data)
               }else {
                   reject(request.data)
               }
           },
           fail(error) {
             console.log(error)
               reject(error.data)
           }
       })
   })
}

const get = (url, options = {}) => {
   return request(url, { method: 'GET', data: options })
}

const post = (url, options) => {
   return request(url, { method: 'POST', data: options })
}

const put = (url, options) => {
   return request(url, { method: 'PUT', data: options })
}

// 不能声明DELETE（关键字）
const remove = (url, options) => {
   return request(url, { method: 'DELETE', data: options })
}

module.exports = {
   get,
   post,
   put,
   remove
}